{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyPi5Ho7I9ICMU0Jnn+/aVjA"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "## Function Calling\n",
        "\n",
        "The main purpose of using Function Calling with large models is to expand their capabilities, allowing them to integrate more information and functionality when generating text, making decisions, or performing tasks. By defining and calling external functions, large models can handle more complex tasks like performing mathematical calculations, placing orders, or querying databases, thereby enhancing their practicality and flexibility.\n"
      ],
      "metadata": {
        "id": "tY2fLItjnn6U"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 🌟 Using LlamaIndex\n",
        "\n",
        "LlamaIndex has developed an easy-to-use method for Function Calling. Next, we'll use yi-large for a demo.\n",
        "\n",
        "First, install the necessary dependencies. Follow along step by step!\n"
      ],
      "metadata": {
        "id": "EiQrQO8In5W2"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install llama-index\n",
        "!pip install llama-index-llms-huggingface\n",
        "!pip install llama-index-llms-yi"
      ],
      "metadata": {
        "id": "9-f7jfstpCEe"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Make sure the installation is complete before we proceed.\n",
        "\n",
        "Now, let's load the dependencies.\n"
      ],
      "metadata": {
        "id": "3tlgHR5spMhG"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "w9F41myGnfCS",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1721973909886,
          "user_tz": -480,
          "elapsed": 8874,
          "user": {
            "displayName": "haijian wang",
            "userId": "16545674694152900117"
          }
        }
      },
      "outputs": [],
      "source": [
        "from typing import Any\n",
        "\n",
        "from llama_index.core.llms import (\n",
        "    CustomLLM,\n",
        "    CompletionResponse,\n",
        "    CompletionResponseGen,\n",
        "    LLMMetadata,\n",
        ")\n",
        "from llama_index.core.llms.callbacks import llm_completion_callback\n",
        "from llama_index.llms.yi import Yi\n",
        "\n",
        "from llama_index.core.tools import FunctionTool\n",
        "from llama_index.core.agent import ReActAgent"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Next, configure the model. You'll need to apply for an api_key on the [01.ai](https://platform.01.ai/apikeys).\n"
      ],
      "metadata": {
        "id": "xBu4fAuapTMS"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "llm = Yi(model=\"yi-large\", api_key=\"your_api_key\")"
      ],
      "metadata": {
        "id": "Lq4QFDl-pLn_"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now, let's define our tools. Here, we'll create functions for addition, subtraction, and multiplication.\n"
      ],
      "metadata": {
        "id": "gCnRQ2vNpViR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Define tools\n",
        "def multiply(a: int, b: int) -> int:\n",
        "    \"\"\"Multiply two integers and return the result\"\"\"\n",
        "    return a * b\n",
        "\n",
        "def plus(a: int, b: int) -> int:\n",
        "    \"\"\"Add two integers and return the result\"\"\"\n",
        "    return a + b\n",
        "def minus(a: int, b: int) -> int:\n",
        "    \"\"\"Subtract two integers and return the result.\"\"\"\n",
        "    return a - b\n",
        "multiply_tool = FunctionTool.from_defaults(fn=multiply)\n",
        "plus_tool = FunctionTool.from_defaults(fn=plus)\n",
        "minus_tool = FunctionTool.from_defaults(fn=minus)"
      ],
      "metadata": {
        "id": "g5-zAq22pXu6"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Next, let's create our Agent.\n"
      ],
      "metadata": {
        "id": "cvUFnsAkph-O"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "agent = ReActAgent.from_tools([multiply_tool,plus_tool,minus_tool], llm=llm, verbose=True)"
      ],
      "metadata": {
        "id": "Hurvg2J1pjQB"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Once everything is set up correctly, we can start our conversation.\n"
      ],
      "metadata": {
        "id": "kEkZMzaXplsK"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "while True:\n",
        "    user_input = input(\"user>>\") # \"(1+2)*10\"\n",
        "    agent.chat(user_input)"
      ],
      "metadata": {
        "id": "Ki3NKbq_pqTz"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Let's take a look at the result.\n"
      ],
      "metadata": {
        "id": "HjcL_f7npr-K"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "``````\n",
        "user>>(1+2)*10\n",
        "Thought: The user's question is in English. I need to use a combination of the 'plus' and 'multiply' tools to solve the mathematical expression (1+2)*10.\n",
        "Action: plus\n",
        "Action Input: {'a': 1, 'b': 2}\n",
        "Observation: 3\n",
        "Thought: I have the result of the 'plus' operation, which is 3. Now I need to multiply this result by 10 to complete the expression (1+2)*10.\n",
        "Action: multiply\n",
        "Action Input: {'a': 3, 'b': 10}\n",
        "Observation: 30\n",
        "Thought: I have the result of the 'multiply' operation, which is 30. This is the final result of the expression (1+2)*10.\n",
        "Thought: I can answer without using any more tools. I'll use the user's language to answer.\n",
        "Answer: The result of the expression (1+2)*10 is 30.\n",
        "``````"
      ],
      "metadata": {
        "id": "oBUz2VtkpuUw"
      }
    }
  ]
}
